package com.imax.center.controller;

import com.imax.center.annotation.NoAuth;
import com.imax.center.service.CommonService;
import com.imax.center.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 图片预览控制器
 */
@Api(tags = "图片预览")
@RestController
@RequestMapping("/image")
@Slf4j
public class ImageController {

    @Autowired
    private CommonService commonService;

    @Autowired
    MinioService minioService;

    /**
     * 根据路径下载图片
     * @param url 图片路径
     * @param response HTTP响应
     */
    @NoAuth
    @ApiOperation("下载图片")
    @GetMapping("/download")
    public void downloadImage(
            @ApiParam(value = "图片路径", required = true) @RequestParam String url,
            HttpServletResponse response) {
        
        InputStream inputStream = null;
        
        try {
            // 调用服务层方法获取图片流
            inputStream = commonService.getImageInputStream(url);
            
            if (inputStream == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片不存在");
                return;
            }
            
            // 获取文件名
            String fileName = new File(url).getName();
            
            // 设置响应头
            response.setContentType("image/jpeg");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            
            // 将输入流复制到响应输出流
            StreamUtils.copy(inputStream, response.getOutputStream());
            response.flushBuffer();
            
            log.info("图片 '{}' 下载成功", fileName);
        } catch (Exception e) {
            log.error("下载图片时发生异常", e);
            try {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "下载图片时发生异常: " + e.getMessage());
            } catch (IOException ex) {
                log.error("发送错误响应失败", ex);
            }
        } finally {
            // 关闭资源
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("关闭输入流失败", e);
                }
            }
        }
    }

    @NoAuth
    @ApiOperation("下载minio图片")
    @GetMapping("/downloadMinio")
    public void downloadMinio(
            @ApiParam(value = "下载minio图片", required = true) @RequestParam String url,
            HttpServletResponse response) {

        InputStream inputStream = null;

        try {
            // 调用服务层方法获取图片流
            inputStream = minioService.download(url);

            if (inputStream == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片不存在");
                return;
            }

            // 获取文件名
            String fileName = new File(url).getName();

            // 设置响应头
            response.setContentType("image/jpeg");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

            // 将输入流复制到响应输出流
            StreamUtils.copy(inputStream, response.getOutputStream());
            response.flushBuffer();

            log.info("图片 '{}' 下载成功", fileName);
        } catch (Exception e) {
            log.error("下载图片时发生异常", e);
            try {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "下载图片时发生异常: " + e.getMessage());
            } catch (IOException ex) {
                log.error("发送错误响应失败", ex);
            }
        } finally {
            // 关闭资源
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("关闭输入流失败", e);
                }
            }
        }
    }

} 